package eu.ahsx.limboa.media
{
	import com.demonsters.debugger.MonsterDebugger;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	

	/***
	 * 	AVM2Loader
	 * 	
	 * 	@author Masy Alexandre
	 * 	@private
	 * */
	internal class AVM2Loader extends EventDispatcher implements IAssetsLoader
	{
		public var l:Loader;
		
		protected var file:MediaFile;
		protected var debug:Boolean;
		
		public function AVM2Loader(debug:Boolean = false)
		{
			this.debug = debug;
			super();
		}
		
		public function load(file:MediaFile):void
		{
			this.file = file;
			
			if (debug)
				MonsterDebugger.trace("[ASSETS] ", file.id + " INIT");
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.INIT));
			
			l = new Loader();
			l.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete, false, 0, true);
			l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress, false, 0, true);
			l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError, false, 0, true);
			l.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError, false, 0, true);
			l.load( new URLRequest(file.url) );
			if (debug)
				MonsterDebugger.trace("[ASSETS] ", file.id + " START");
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.START));
		}
		
		public function get percent():int
		{
			if (l)
				return l.contentLoaderInfo.bytesLoaded/l.contentLoaderInfo.bytesTotal;
			else
				return 0;
		}
		
		public function get data():*
		{
			if (l && l.content)
				return l.content as MovieClip;
			else
				return null;
		}
		
		public function stop():void
		{
			if (l)
			{
				try
				{
					l.close();
					l.unload();
				}
				catch(e:Error)
				{
					
				}
			}
			
			if (debug)
				MonsterDebugger.trace("[ASSETS] ", file.id + " ERROR "+ "Stop the loading");
				
			file.info.error = new ErrorEvent(ErrorEvent.ERROR, false, false, "Stop the loading");
	
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.ERROR));
			
		}
		
		public function unload():void
		{
			stop();
			if (l)
				l.unloadAndStop();
		}
		
		protected function loadComplete(event:Event):void
		{
			if (debug)
				MonsterDebugger.trace("[ASSETS] ", file.id + " COMPLETE");
			
			l.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete, false);
			l.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgress, false);
			l.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loadError, false);
			l.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError, false);
			
			file.info.domain = l.contentLoaderInfo.applicationDomain;
			
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.COMPLETE));
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.DONE));
		}
		
		protected function loadProgress(event:ProgressEvent):void
		{
			if (debug)
				MonsterDebugger.trace("[ASSETS] ", file.id + " PROGRESS");
			file.info.bytesloaded = event.bytesLoaded;
			file.info.bytesTotal = event.bytesTotal;
			file.info.percent = event.bytesLoaded/event.bytesTotal;
			
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.PROGRESS));
		}
		
		protected function loadError(event:ErrorEvent):void
		{
			if (debug)
				MonsterDebugger.trace("[ASSETS] ", file.id + " ERROR "+ event.text);
			
			file.info.error = event;
			
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.ERROR));
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.DONE));
			
		}
	}
}